package com.pfzy.ui.activity.refresh.refresh2;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.XRefreshViewFooter;
import com.pfzy.ui.R;
import com.pfzy.ui.databinding.ActivityRefreshXBinding;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RefreshXActivity extends AppCompatActivity implements View.OnClickListener {
    private ActivityRefreshXBinding mBind;
    private SimpleAdapter mAdapter;
    private List<Person> personList = new ArrayList<Person>();
    private int mLoadCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBind = ActivityRefreshXBinding.inflate(getLayoutInflater());
        setContentView(mBind.getRoot());
        initView();
    }

    private void initView() {
        TextView title = mBind.toolbar.findViewById(R.id.layout_toolbar_title);
        title.setText("刷新");
        mBind.toolbar.findViewById(R.id.layout_toolbar_back).setOnClickListener(this);

        for (int i = 0; i < 10; i++) {
            Person person = new Person("name" + i, "" + i);
            personList.add(person);
        }
        mAdapter = new SimpleAdapter(personList, this);
        mBind.activityRefreshRecycler.setLayoutManager(new LinearLayoutManager(this));
        mBind.activityRefreshRecycler.setAdapter(mAdapter);

        mBind.swipeRefreshLayout.setPullLoadEnable(true);
        CustomGifHeader header = new CustomGifHeader(this);
        mBind.swipeRefreshLayout.setCustomHeaderView(header);
        mBind.swipeRefreshLayout.setAutoLoadMore(false);
        mBind.swipeRefreshLayout.setPinnedTime(1000);
        mBind.swipeRefreshLayout.setMoveForHorizontal(true);
        mBind.swipeRefreshLayout.setPullLoadEnable(true);
        mAdapter.setCustomLoadMoreView(new XRefreshViewFooter(this));
//        xRefreshView1.setPullRefreshEnable(false);
        //设置在下拉刷新被禁用的情况下，是否允许界面被下拉,默认是true
//        xRefreshView1.setMoveHeadWhenDisablePullRefresh(false);
//        xRefreshView1.enablePullUpWhenLoadCompleted(false);
//		xRefreshView1.setPullLoadEnable(false);
//        xRefreshView1.enableRecyclerViewPullUp(false);
        //设置静默加载时提前加载的item个数
//		xRefreshView1.setPreLoadCount(2);

        mBind.swipeRefreshLayout.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //模拟数据加载失败的情况
                        Random random = new Random();
                        boolean success = random.nextBoolean();
                        if (success) {
                            mBind.swipeRefreshLayout.stopRefresh();
                        } else {
                            mBind.swipeRefreshLayout.stopRefresh(false);
                        }
                        //或者
//                        xRefreshView1.stopRefresh(success);
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        if(mBind.swipeRefreshLayout.hasLoadCompleted()){
                        }
                        for (int i = 0; i < 6; i++) {
                            mAdapter.insert(new Person("More ", "21"),
                                    mAdapter.getAdapterItemCount());
                        }
                        mLoadCount++;
                        if (mLoadCount >= 3) {
                            mBind.swipeRefreshLayout.setLoadComplete(true);
                        } else {
                            // 刷新完成必须调用此方法停止加载
                            mBind.swipeRefreshLayout.stopLoadMore(false);
                        }
                    }
                }, 1000);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_toolbar_back:
                finish();
                break;
            default:
                break;
        }
    }
}
